<template>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="订单编号" prop="id">
        <el-input readonly v-model="ruleForm.id"></el-input>
    </el-form-item>
    <el-form-item label="茶叶商品" prop="good">
        <el-input v-model="ruleForm.good"></el-input>
    </el-form-item>
    <el-form-item label="购买者id" prop="purchaserid">
        <el-input type="number" v-model="ruleForm.purchaserid" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="订单明细" prop="info">
        <el-input type="text" v-model="ruleForm.info" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
        <el-button @click="returnForm">返回列表</el-button>
    </el-form-item>
</el-form>
</template>
<script>
import {searchGoods,updateGoods} from '@/api'
  export default {
    created() {
        let id = this.$route.query.id
        //console.log(id)
        let _this = this
        searchGoods(id).then((res) => {
            _this.ruleForm = res.data
        })
    },
    data() {
      return {
        ruleForm: {
          id:0,
          good: '',
          purchaserid:0,
          info: '',
        },
        rules: {
          good: [
            { required: true, trigger: 'blur' }
          ],
          purchaserid: [
            { required: true, trigger: 'blur' }
          ],
          info: [
            { required: true, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        let _this = this;
        this.$refs[formName].validate((valid) => {
          if (valid) {
            updateGoods(_this.ruleForm).then((res) => {
                if(res){
                    _this.$alert('订单： ' + _this.ruleForm.id+' 修改成功！', '修改数据', {
                        confirmButtonText: '确定',
                        callback: action => {
                            //跳转
                            _this.$router.push('/goodslist')
                        }
                    });
                }
            })
          } else {
            this.$message.error('请补充信息！');
            return false;
          }
        });
      },
      returnForm() {
        this.$router.push('/goodslist')
      }
    }
  }
</script>